<?php
/**
*File Name:MovietermController.php
*Date:2016年3月1日上午10:33:58
*Copyright  (c)2016,783188184@qq.com All Rights Reserved
*____________________________________________________________________________________________________
*/
	namespace Admin\Controller;
	use Think\Controller;
	class MovietermController extends Controller{
		//栏目呈现和添加栏目
		public function termlist(){
			$m = M('vterm');
			$res = $m->order(array("id"=>"asc"))->select();
			foreach ($res as $value){
				$a['id'] = $value['id'];
				$a['parentid'] = $value['p_id'];
				$a['name'] = $value['term_name'];
				$a['ename'] = $value['term_ename'];
				$url = "'".U("movieterm/delete", array("id" => $a['id'])). "'";
				$a['html'] = '<a class="btn btn-primary" href="' . U("movieterm/termedit", array("id" => $a['id'])) . '">'.'编辑'.'</a>'.'&nbsp;&nbsp;'
				.'<a class="btn btn-danger" onClick="delcfm('."$url".')">删除</a>';
				$c[] = $a;
			}
            //用了thinkcmf中的树生成类注意的是这里给类的数组要和其给的数组键名一致否则无效
			$tree = new \Org\Util\Tree();
			//建两个类是因为有两处要用且形式不一样
			$addtree = new \Org\Util\Tree();
			$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
			$addttree->icon = $tree->icon;
			$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
			$addtree->nbsp = $tree->nbsp;
			$tree->init($c);
			$addtree->init($c);
			//$spacer是空格和形式符
			$str = "<tr>
		      <td>\$spacer <input type='checkbox' class='term' name='ids[]' value='\$id'>\$id.\$name</td>
		      <td>创建子类</td>
		      <td><input type='text' value='\$name' name='term_name[\$id]'/></td>
		      <td><input type='text' value='\$ename' name='term_ename[\$id]'/></td>
		      <td>\$html</td>
		    </tr>";
			$addstr = "<option value='\$id'>\$spacer\$name</option>";
			$tr = $tree->get_tree(0, $str);
			$addlist= $addtree->get_tree(0,$addstr);
			$this->assign('termlist',$tr);
			$this->assign('addlist',$addlist);
			$this->display();				
		}
		
		
		//增加栏目数据接收
		public function addterm_post(){
			$vterm = M('vterm');
			if(IS_POST){
				$vterm->create();
				$res = $vterm->add();
				if($res){
					$this->success('新增成功', U('Movieterm/termlist'));
				}else{
					$this->error('添加失败', U('Movieterm/termlist'));
				}
			}
		}
		
		//栏目修改
		public function termedit(){
			$id = $_GET['id'];
			$m = M('vterm');
			$res = $m->order(array("id"=>"asc"))->select();
			//查询得到所属父类
			$data= $m->where(array("id" => $id))->find();
			foreach ($res as $value){
				$a['id'] = $value['id'];
				$a['parentid'] = $value['p_id'];
				$a['name'] = $value['term_name'];
				$a['ename'] = $value['term_ename'];
				$url = "'".U("movieterm/delete", array("id" => $a['id'])). "'";
				$a['html'] = '<a class="btn btn-primary" href="' . U("movieterm/termedit", array("id" => $a['id'])) . '">'.'编辑'.'</a>'.'&nbsp;&nbsp;'
				.'<a class="btn btn-danger" onClick="delcfm('."$url".')">删除</a>';
				$a['selected'] = $data['p_id']==$a['id'] ? "selected":'';
				$c[] = $a;
			}
			//用了thinkcmf中的树生成类注意的是这里给类的数组要和其给的数组键名一致否则无效
			$tree_edit = new \Org\Util\Tree();
			$tree_edit->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
			$tree_edit->nbsp = '&nbsp;&nbsp;&nbsp;';
			$tree_edit->init($c);
			$str = "<option value='\$id' \$selected>\$spacer\$name</option>";
			$tr = $tree_edit->get_tree(0, $str);
			//生成分类列表树
			$tree_list = new \Org\Util\Tree();
			$tree_list->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
			$tree_list->nbsp = '&nbsp;&nbsp;&nbsp;';
			$tree_list->init($c);
			$list_str = "<tr>
		      <td>\$spacer <input type='checkbox' class='term' name='ids[]' value='\$id'>\$id.\$name</td>
		      <td>创建子类</td>
		      <td><input type='text' value='\$name' name='term_name[\$id]'/></td>
		      <td><input type='text' value='\$ename' name='term_ename[\$id]'/></td>
		      <td>\$html</td>
		    </tr>";
			$list = $tree_list->get_tree(0,$list_str);
			$this->assign('termlist',$list);
			$this->assign('editlist',$tr);
			$this->assign('info',$data);
			$this->display();
		}
		
		//栏目重新修改数据接收
		public function edit_post(){
			$m = M('vterm');
			if(IS_POST){
				$m->create();
				if($m->save()){
					$this->success('修改成功',U('movieterm/termlist'));
				}else{
					$this->error('修改失败',U('movieterm/termlist'));
				}
			}else{
				$this->error('请用正确的方式提交',U('index/index'));
			}
		}
		
		//栏目删除并且删除对应栏目下的数据
		public function delete(){
			$m = M('vterm');
			if(IS_GET){
				$id = $_GET['id'];
				$count = $m->where(array("p_id"=>$id))->count();
				if($count > 0){
					$this->error("该菜单下还有子类，无法删除！");
				}
				$res = $m->where(array("id"=>$id))->delete();
				if($res){
					$datares = $this->deletedata($id);//删除对应栏目下的类容
					$this->success('删除成功');
				}else{
					$this->error('删除失败');
				}					
			}else{
				$this->redirect('movieterm/termlist',4, '请从正确的方式提出删除要求');
			}
		}
		
		//删除对应栏目下面的数据
		public function deletedata($v_pid){
			$m = M('movie');
			$datares = $m->where('v_pid='. $v_pid)->delete();
			return $datares;
		}
		
		//批量删除栏目并且删除栏目下面的数据
		public function deleteall(){
			if(empty($_POST['ids'])){
				$this->error('请选择要删除的栏目');
			}
			$m = M('vterm');
			$tarray = $_POST['ids'];
			foreach($tarray as $k => $value){
				$arr = $m->where('p_id='.$value)->select();
				$count = count($arr);
				if($count > 0){
					$this->error("ID为".$value."的栏目下还有子类请先删除");
				}else{
					$m->where('id='.$value)->delete();
					$this->deletedata($value);
				}
			}
			$this->success('批量删除成功');
		}
		
		//批量修改
		public function updateall(){
			if(empty($_POST['ids'])){
				$this->error('请选择要修改的栏目');
			}
			$m = M('vterm');
			$tarray = $_POST['ids'];
			foreach($tarray as $k => $value){
				$data['id'] = $value;
				$data['term_name'] = $_POST['term_name'][$value];
				$data['term_ename'] = $_POST['term_ename'][$value];
				$m->where('id='.$value)->save($data);
			}
			$this->success('修改成功');
		}
	}
		